<?php

namespace App\V3\V3_2\Controllers;

use App\V3\V3_2\Services\LogService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;

class LogController extends BaseController
{
    /**
     * @操作记录详情
     * @created 在 2021/12/2 9:32 创建
     * @updated at 2022/06/05 13:48 By Sun Wu
     * @param Request $request
     * @author: Sun Wu
     */
    public function detail(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'id' =>'required|integer',//操作日志主键id
            'type' => [
                'required',
                Rule::In([0, 1, 2]),
            ],//操作日志类型;0:入库;1:出库;2:套餐出库;
        ]);

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }

        $data['user_id'] = $this->getUserId();
        $data['main_id'] = $this->getMainUserId();

        return $this->success(
            LogService::detail($data)
        );
    }

    public function logDel(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'log_id' => 'required',
            'type' => [
                'required',
                Rule::In([0, 1, 2]),
            ],//操作日志类型;0:入库;1:出库;2:套餐出库;
        ],[
            'log_id.required' => '记录id缺失',
            'type.required' => '记录id缺失',
            'type.In' => '日志类型错误',
        ]);

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }

        $logIdArr = array_filter(explode(',', $data['log_id']), function($v) {
            return $v !== '';
        });

        $condition = [];
        if($this->getMainUserId() == $this->getUserId()) {
            $condition['main_id'] = ['EQ' => $this->getMainUserId()];
        }
        else {
            $condition['user_id'] = ['EQ' => $this->getUserId()];
        }

        $data['main_id'] = $this->getMainUserId();

        return response()->json(
            LogService::logDel($data, $logIdArr, $condition)
        );
    }

    public function list(Request $request) {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'pageSize' =>'required|integer',//页面大小
            'currentPage' =>'required|integer',//当前页
            'type' => [
                'required',
                Rule::In([0, 1, 2]),
            ],//操作日志类型;0:入库;1:出库;2:套餐出库;
        ]);

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }

        //撤销状态;0:未撤销;1:已撤销;
        $data['status'] = !isset($data['status'])?0:$data['status'];
        $data['is_share_log'] = $this->isShareLog();
        $data['is_share_storage'] = $this->isShareStorage();
        $data['main_id'] = $this->getMainUserId();
        $data['user_id'] = $this->getUserId();

        return $this->success(
            LogService::list($data)
        );
    }

    /**----abandon
     * @操作记录列表
     * @created 在 2021/12/1 14:21 创建
     * @param Request $request
     * @author: Sun Wu
     */
    public function list1(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'pageSize' =>'required|integer',//页面大小
            'currentPage' =>'required|integer',//当前页
            'type' => [
                'required',
                Rule::In([0, 1, 2]),
            ],//操作日志类型;0:入库;1:出库;2:套餐出库;
        ]);

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }

        if($this->getUserId() == $this->getMainUserId()) {
            $userId['f.main_id'] = $this->getMainUserId();
        }
        else {
            if($this->isShareLog()) {
                $userId['f.main_id'] = $this->getMainUserId();
            }
            else {
                $userId['f.user_id'] = $this->getUserId();
            }
        }

        //撤销状态;0:未撤销;1:已撤销;
        $status = !isset($data['status'])?0:$data['status'];
       $list = LogService::list($data,$status,$userId);

       if(!empty($list['data'])){

            foreach ($list['data'] as &$v){

                if(!empty($v['serial_number'])){
                    $v['serial_number'] = explode(',', $v['serial_number']);

                }else{
                    $v['serial_number'] =[];
                }
                if(empty($v['shelf_name'])) $v['shelf_name']="";
                if(empty($v['location_name'])) $v['location_name']="";
            }
       }
        return $this->success($list);
    }

    /**
     * 调拨撤销
     * @param Request $request
     * 2022/9/30 16:00
     * Author: Sun Wu
     * @return \Illuminate\Http\JsonResponse
     */
    public function cancalAllocate(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'id' =>'required|integer',//页面大小
            'type' => [
                'required',
                Rule::In([0, 1]),
            ],//撤销类型;0:调入侧撤销;1:调出侧撤销;
        ]);

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail('', $info);
        }

        if($data['type']) {
            $result = LogService::cancelAllocateOut($data);
        }
        else {
            $result = LogService::cancelAllocateIn($data);
        }

        $this->log('调拨撤销', $data);
        return response()->json($result);
    }

    /**
     * @撤销直接入库
     * @created 在 2021/12/1 10:28 创建
     * @param Request $request
     * @author: Sun Wu
     */
    public function cancelDirectInstorage(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->except('role_id'), [
            'id' =>'required|integer',//入库日志主键id
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }

        $data['user_id'] = $this->getUserId();
        $data['main_id'] = $this->getMainUserId();

        $this->log('直接入库撤销', $data);

        return response()->json(
            LogService::cancelDirectInstorage($data)
        );
    }

    /**
     * @直接出库撤销
     * @created 在 2021/12/17 10:37 创建
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @author: Sun Wu
     */
    public function cancelDirectOutstorage(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'id' =>'required|integer',//操作日志主键id
        ]);

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }

        $this->log('撤销直接出库', $data);
        return response()->json(
            LogService::cancelDirectOutstorage($data)
        );
    }

    /**
     * @套餐出库撤销
     * @created 在 2021/12/16 17:28 创建
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @author: Sun Wu
     */
    public function cancelPackage(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'id' =>'required|integer',//操作日志主键id
        ]);

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }

        $this->log('撤销套餐出库', $data);
        return response()->json(
            LogService::cancelPackage($data)
        );
    }

    /**
     * 旧操作记录列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @author ソンブ
     * @created_at: 2022/2/9 15:28
     */
    public function oldLogList(Request $request)
    {
        return response()->json(
            LogService::oldLogList(
                $request->type,
                $request->start_time,
                $request->end_time,
                $request->curr_page,
                $request->page_size,
                $this->getUserId(),
                $this->getMainUserId()
            )
        );
    }

    public function cancelOldLog(Request $request)
    {
        return response()->json(
            LogService::cancelOldLog(
                $request->id,
                $this->getUserId(),
                $this->getMainUserId()
            )
        );
    }
}
